package render

import (
	"MetroBuilderGameJam/gameEngine/geom"
	"MetroBuilderGameJam/world"
)

func newSlotOffsetCalculator(lineWidth float64) *slotOffsetCalculator {
	return &slotOffsetCalculator{
		lineWidth: lineWidth,
	}
}

type slotOffsetCalculator struct {
	lineWidth float64
}

func (s *slotOffsetCalculator) getFromOffset(branch world.RailRoadBranch) geom.Vec2 {
	side, index := branch.FromStation().GetSlotForBranch(branch)
	return s.slotIndexToOffset(branch.From(), branch.To(), side, index)
}

func (s *slotOffsetCalculator) getToOffset(branch world.RailRoadBranch) geom.Vec2 {
	if branch.ToStation() == nil {
		return geom.Vec2{}
	}
	side, index := branch.ToStation().GetSlotForBranch(branch)
	return s.slotIndexToOffset(branch.To(), branch.From(), side, index)
}

func (s *slotOffsetCalculator) slotIndexToOffset(from *geom.Point, to *geom.Point, side world.RoadSlotSide, index int) geom.Vec2 {
	k := 1
	//if index > 0 && (side == 0 || side == 180) && from.Y < to.Y {
	//	k = -1
	//}
	switch index {
	case 1:
		return (geom.Vec2{X: 1, Y: 0}).Rotate(geom.Degrees(int(side)%180 + 90*k).ToRadians()).Mul(s.lineWidth)
	case 2:
		return (geom.Vec2{X: -1, Y: 0}).Rotate(geom.Degrees(int(side)%180 + 90*k).ToRadians()).Mul(s.lineWidth)
	}
	return geom.Vec2{}
}
